<!--车辆处置页面-->
<template>
  <div>
    <el-dialog
      :title="doEdit?'操作':'查看'"
      :visible.sync="dialogVisible"
      width="80%"
      v-if="formData"
      @close="close"
    >
      <el-form :model="formData" label-width="150px" ref="form" :rules="rules">
        <el-row>
          <el-col :sm="12">
            <el-form-item label="单位名称">{{formData.company.companyName}}</el-form-item>
          </el-col>
          <el-col :sm="12">
            <el-form-item label="处置方式">{{formData.clname}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="24">
            <el-form-item label="附件">
              <a @click="download(formData.fileurl,formData.filename)">{{formData.filename}}</a>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :sm="24">
            <el-form-item label="拍卖公司" prop="pmgs">
              <el-input v-model="formData.pmgs" placeholder="请输入拍卖公司" v-show="doEdit"></el-input>
              <span v-show="!doEdit">{{formData.pmgs}}</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row style="margin-top:20px;">
          <el-col :sm="24">
            <el-form-item label="处置车辆">
              <el-table :data="formData.cardisposecars" style="width: 100%" size="mini" border>
                <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
                <el-table-column prop="car.carCode" label="车牌号" align="center"></el-table-column>
                <el-table-column prop="car.pinpai" label="品牌" align="center"></el-table-column>
                <el-table-column prop="car.carColor" label="颜色" align="center"></el-table-column>
                <el-table-column prop="car.useType" label="使用性质" align="center"></el-table-column>
                <!-- <el-table-column label="操作" align="center">
                  <el-button type="primary" size="mini">查看</el-button>
                </el-table-column>-->
              </el-table>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row style="margin-top:10px;">
          <el-col>
            <el-form-item label="拍卖成交确认书" prop="fileurl1">
              <el-upload
                v-show="doEdit"
                class="upload-mini"
                drag
                :action="webUploadUrl"
                :on-success="(response, file, fileList)=>onSuccess1(response, file, fileList,'fileurl1','filename1')"
                :file-list="formData.fileurl1list"
                :limit="1"
                :on-exceed="onExceed"
                :on-remove="onRemove1('fileurl1','filename1')"
              >
                <div class="el-upload__text">
                  将文件拖到此处，或
                  <em>点击上传</em>
                </div>
              </el-upload>
              <a
                v-show="!doEdit"
                @click="download(formData.fileurl1,formData.filename1)"
              >{{formData.filename1}}</a>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="评估报告" prop="fileurl2">
              <el-upload
                v-show="doEdit"
                class="upload-mini"
                drag
                :action="webUploadUrl"
                :on-success="(response, file, fileList)=>onSuccess1(response, file, fileList,'fileurl2','filename2')"
                :limit="1"
                :on-exceed="onExceed"
                :file-list="formData.fileurl2list"
                :on-remove="onRemove1('fileurl2','filename2')"
              >
                <div class="el-upload__text">
                  将文件拖到此处，或
                  <em>点击上传</em>
                </div>
              </el-upload>
              <a
                v-show="!doEdit"
                @click="download(formData.fileurl2,formData.filename2)"
              >{{formData.filename2}}</a>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="拍卖公告" prop="fileurl3">
              <el-upload
                v-show="doEdit"
                class="upload-mini"
                drag
                :action="webUploadUrl"
                :on-success="(response, file, fileList)=>onSuccess1(response, file, fileList,'fileurl3','filename3')"
                :limit="1"
                :file-list="formData.fileurl3list"
                :on-exceed="onExceed"
                :on-remove="onRemove1('fileurl3','filename3')"
              >
                <div class="el-upload__text">
                  将文件拖到此处，或
                  <em>点击上传</em>
                </div>
              </el-upload>
              <a
                v-show="!doEdit"
                @click="download(formData.fileurl3,formData.filename3)"
              >{{formData.filename3}}</a>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="拍卖情况报告" prop="fileurl4">
              <el-upload
                v-show="doEdit"
                class="upload-mini"
                drag
                :action="webUploadUrl"
                :on-success="(response, file, fileList)=>onSuccess1(response, file, fileList,'fileurl4','filename4')"
                :limit="1"
                :file-list="formData.fileurl4list"
                :on-exceed="onExceed"
                :on-remove="onRemove1('fileurl4','filename4')"
              >
                <div class="el-upload__text">
                  将文件拖到此处，或
                  <em>点击上传</em>
                </div>
              </el-upload>
              <a
                v-show="!doEdit"
                @click="download(formData.fileurl4,formData.filename4)"
              >{{formData.filename4}}</a>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="买受人身份证复印件" prop="fileurl5">
              <el-upload
                v-show="doEdit"
                class="upload-mini"
                drag
                :action="webUploadUrl"
                :file-list="formData.fileurl5list"
                :on-success="(response, file, fileList)=>onSuccess1(response, file, fileList,'fileurl5','filename5')"
                :limit="1"
                :on-exceed="onExceed"
                :on-remove="onRemove1('fileurl5','filename5')"
              >
                <div class="el-upload__text">
                  将文件拖到此处，或
                  <em>点击上传</em>
                </div>
              </el-upload>
              <a
                v-show="!doEdit"
                @click="download(formData.fileurl5,formData.filename5)"
              >{{formData.filename5}}</a>
            </el-form-item>
          </el-col>
        </el-row>
        <!--<el-row>
          <el-col :sm="24">
            <el-form-item label="历史流程"></el-form-item>
          </el-col>
        </el-row>
         <el-timeline :reverse="true" class>
          <el-timeline-item
            v-for="(activity, index) in history"
            :key="index"
            :timestamp="activity.optime"
          >
            {{activity.taskName}}。操作人：
            {{activity.userName}}，
            备注：
            {{activity.mark||' - '}}。
            <template>
              <span v-if="activity.passStr">--------- {{activity.passStr}}</span>
            </template>
          </el-timeline-item>
        </el-timeline>-->
      </el-form>
      <span slot="footer" class="dialog-footer" style="text-align: left;">
        <el-button @click="close">关闭</el-button>
        <el-button type="primary" @click="saveczss" v-if="doEdit&&type==0">暂存，下次继续</el-button>
        <el-button type="primary" @click="save" v-if="doEdit&&type==0">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { viewJson, saveczss, dwczss } from "@/api/task";
import { dateFormat } from "@/utils/utils";
export default {
  data() {
    return {
      pass: 1,
      opData: null,
      history: [],
      formData: null,
      doEdit: false,
      dialogVisible: false,
      type: 0, //0待办任务 2历史任务
      webUploadUrl: "", //文件上传地址
      rules: {
        pmgs: [{ required: true, message: "请输入拍卖公司", trigger: "blur" }],
        fileurl1: [{ required: true, message: "请上传文件", trigger: "blur" }],
        fileurl2: [{ required: true, message: "请上传文件", trigger: "blur" }],
        fileurl3: [{ required: true, message: "请上传文件", trigger: "blur" }],
        fileurl4: [{ required: true, message: "请上传文件1", trigger: "blur" }],
        fileurl5: [{ required: true, message: "请上传文件", trigger: "blur" }],
      },
    };
  },
  created() {
    this.webUploadUrl = this.VUE_APP_BASE_API + "/common/webupload.shtml";
  },
  methods: {
    onExceed() {
      this.$message("只能上传1个文件，请先移除已上传文件再上传");
    },
    onSuccess1(response, file, fileList, fileurlKey, filenameKey) {
      if (!response) return;
      console.log(response);
      if (response.error == 0) {
        this.formData[fileurlKey] = response.url;
        this.formData[filenameKey] = response.filename;
        console.log(this.formData);
      } else {
        this.$message("上传失败，请重试");
      }
    },
    onRemove1() {
      // this.applyForm.bzwjurl = "";
    },
    close() {
      this.dialogVisible = false;
    },
    /**
     * @description 操作任务
     * @param opData 当前操作的任务原始数据 list接口返回的
     * @param type 当前列表的类型 0待办任务 2历史任务 2类型时候，只有调度员可编辑且不能再修改备注了
     * @param taskDetail 任务详情 父组件通过viewJson获取传过来
     */
    editTask(opData, type, taskDetail) {
      this.viewTask(opData, type, taskDetail);
      this.doEdit = true;
      this.tabIndex = "3";
      this.type = type;
    },
    /**
     * @description 查看任务
     * @param opData 当前操作的任务原始数据 list接口返回的
     * @param type 当前列表的类型 0待办任务 2历史任务 2类型时候，只有调度员可编辑且不能再修改备注了
     * @param taskDetail 任务详情 父组件通过viewJson获取传过来
     */
    viewTask(opData, type, taskDetail) {
      this.type = type;
      this.doEdit = false;
      this.opData = opData;
      let params = {
        tableId: opData.tableId,
        pid: opData.processInstanceId,
        taskId: opData.taskId,
        formKey: opData.formKey,
      };
      if (taskDetail.data.cardisposecars && taskDetail.data.cardisposecars[0]) {
        for (let key in taskDetail.data.cardisposecars[0]) {
          if (key.startsWith("file")) {
            taskDetail.data[key] = taskDetail.data.cardisposecars[0][key];
          }
          if (key.startsWith("fileurl")) {
            if (taskDetail.data.cardisposecars[0][key]) {
              let filenameKey = "filename" + key.substring(key.length - 1);
              taskDetail.data[key + "list"] = [
                {
                  name: taskDetail.data.cardisposecars[0][filenameKey],
                  url: taskDetail.data.cardisposecars[0][key],
                },
              ];
            } else {
              taskDetail.data[key + "list"] = [];
            }
          }
        }
        taskDetail.data.pmgs = taskDetail.data.cardisposecars[0].pmgs;
      } else {
        this.$message({
          message: "没有车辆数据",
          type: "error",
        });
        return;
      }
      this.formData = taskDetail.data;
      this.formData.fileurl = this.VUE_APP_BASE_API + this.formData.fileurl;
      this.history.splice(0, this.history.length, ...taskDetail.history);
      this.dialogVisible = true;
    },
    //处理流程
    async saveczss() {
      //   this.$refs["form"].validate(async valid => {
      //     if (valid) {
      //     } else {
      //       return false;
      //     }
      //   });

      let param = {};
      param["pass"] = this.pass ? "true" : "false";
      param["mark"] = this.formData.shenhemark || "";
      param["tableId"] = this.opData.tableId;
      param["taskId"] = this.opData.taskId;
      param["formKey"] = this.opData.formKey;
      param["pid"] = this.opData.processInstanceId;
      for (let key in this.formData) {
        param[key] = this.formData[key];
      }
      let d = await saveczss(param);
      if (d.error == 0) {
        this.$message("操作成功");
        this.finish();
      } else {
        this.$message("操作失败");
      }
    },
    //处理流程
    save() {
      console.log(this.formData);
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          let param = {};
          param["pass"] = this.pass ? "true" : "false";
          param["mark"] = this.formData.shenhemark || "";
          param["tableId"] = this.opData.tableId;
          param["taskId"] = this.opData.taskId;
          param["formKey"] = this.opData.formKey;
          param["pid"] = this.opData.processInstanceId;
          for (let key in this.formData) {
            param[key] = this.formData[key];
          }
          let d = await dwczss(param);
          if (d.error == 0) {
            this.$message("操作成功");
            this.finish();
          } else {
            this.$message("操作失败");
          }
        } else {
          return false;
        }
      });
    },
    //审核
    async sh(params) {
      let d = await sh(params);
      if (d.error == 0) {
        this.$message("操作成功");
        this.finish();
      } else {
        this.$message("操作失败");
      }
    },
    //各种操作完成后关闭对话框刷新父组件列表
    finish() {
      this.close();
      this.$parent.fetchData();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-timeline {
  position: relative;
  top: -25px;
  left: 110px;
  margin-bottom: -45px;
}
</style>
<style lang="scss">
.upload-mini {
  .el-upload-dragger {
    height: 50px;
    line-height: 50px;
  }
}

.upload-mini + .el-form-item__error {
  position: relative;
  top: -15px;
}
</style>